package com.heima.wemedia.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.utils.common.WmThreadLocalUtil;
import com.heima.wemedia.mapper.MaterialMapper;
import com.heima.wemedia.service.MaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @projectName: heima-leadnews
 * @package: com.heima.wemedia.service.impl
 * @className: MaterialServiceImpl
 * @author: 躺着认真玩
 * @description: 素材管理
 * @date: 2025/5/19 4:55
 * @version: 1.0
 */
@Service
@Slf4j
public class MaterialServiceImpl  extends ServiceImpl<MaterialMapper, WmMaterial> implements MaterialService{
    @Autowired
    private FileStorageService fileStorageService;
    @Override
    public ResponseResult upload(MultipartFile multipartFile) {
        // 参数校验
        if (multipartFile == null || multipartFile.getSize() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //生成一个文件名
        String fileName = UUID.randomUUID().toString().replace("-", "");
        String originalFilename = multipartFile.getOriginalFilename();
        String end = originalFilename.substring(originalFilename.lastIndexOf("."));
        // 将文件上传minio
        String url = null;
        try {
            url = fileStorageService.uploadImgFile("", fileName + end, multipartFile.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
            log.error("素材上传失败");
        }
        // 将存储路径存入表中
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUrl(url);
        wmMaterial.setCreatedTime(new DateTime());
        wmMaterial.setType((short) 0);
        wmMaterial.setIsCollection((short)0);
        wmMaterial.setUserId(WmThreadLocalUtil.getUser().getId());
        this.save(wmMaterial);
        return ResponseResult.okResult(wmMaterial);
    }

    @Override
    public PageResponseResult list(WmMaterialDto dto) {
    //     校验参数
        dto.checkParam();
    //     判断是否为收藏
        Page<WmMaterial> page = new Page<>(dto.getPage(), dto.getSize());
        if (dto.getIsCollection() == 1) {
            Page<WmMaterial> page1 = this.lambdaQuery().eq(WmMaterial::getIsCollection, dto.getIsCollection())
                    .orderByDesc(WmMaterial::getCreatedTime)
                    .page(page);

            PageResponseResult pageResponseResult = new PageResponseResult((int) page1.getCurrent(), (int) page1.getSize(), (int) page1.getTotal());
            pageResponseResult.setData(page1.getRecords());
            return pageResponseResult;
        }
        Page<WmMaterial> page1 = this.lambdaQuery()
                .orderByDesc(WmMaterial::getCreatedTime)
                .page(page);

        PageResponseResult pageResponseResult = new PageResponseResult((int) page1.getCurrent(), (int) page1.getSize(), (int) page1.getTotal());
        pageResponseResult.setData(page1.getRecords());
        return pageResponseResult;

    }
}
